package com.gxa.insurance.service.impl;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.mapper.AdminMapper;
import com.gxa.insurance.pojo.Admin;
import com.gxa.insurance.pojo.Permission;
import com.gxa.insurance.service.AdminService;
import com.gxa.insurance.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author Min
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper mapper;

    @Override
    public boolean login(Admin admin) {
        // 将传进来的密码通过加盐加密与 数据库的中密码比对
        Admin dbAdmin = mapper.selectOne(admin);
        if(dbAdmin==null || admin==null){
            return false;
        }
        String password = admin.getPassword();
        String salt = dbAdmin.getSalt();
        password= MD5Util.encode(password+salt);

        if(!dbAdmin.getPhone().equals(admin.getPhone()) || !dbAdmin.getPassword().equals(password)){
            return false;
        }
        //修改上一次的登录时间
        Map<String, Object> HashMap = new HashMap<>();
        HashMap.put("date",new Date(System.currentTimeMillis()));
        HashMap.put("id", dbAdmin.getId());
        Integer isUpdate=mapper.updateLoginTime(HashMap);
        return true;
    }

    @Override
    public boolean findByPhone(String phone) {
        Admin admin=mapper.selectByPhone(phone);
        if (admin==null){
            return false;
        }
        return true;
    }

    @Override
    public List<Permission> selectTreeByphone(String phone) {
        List<Permission> permission=mapper.selectTreeByphone(phone);
        return permission;
    }

    @Override
    public  Map<String,Object> selectAllByPage(String page, String pageSize) {
        Map<String,Object> map = new HashMap<>();
        // 转换
        int intPage=Integer.parseInt(page);
        int intPageSize=Integer.parseInt(pageSize);
        if(intPage<=0){
            intPage=1;
        }else if(intPageSize<=0){
            intPageSize=10;
        }
        intPage=(intPage-1)*intPageSize;
        // 分页查询
        List<Admin> admins = mapper.selectAllByPage(intPage, intPageSize);

        // 查询总条数
        Long count = mapper.selectCount();
        map.put("count",count);
        map.put("list",admins);
        return map;
    }

    @Transactional
    @Override
    public Boolean addAdmin(Admin admin) throws Exception{
        // 先去数据库查询如果手机号存在就结束
        Admin dbAdmin = mapper.selectByPhone(admin.getPhone());
        if(dbAdmin!=null){
            throw new BusinessException(ExceptionCode.DUPLICATE_PHONE);
        }
        if("".equals(admin.getPassword()) || admin.getPassword()==null){
            admin.setPassword("666666");
        }
        // 生产盐值加密用户密码
        String salt = UUID.randomUUID().toString().replace("-", "");
        admin.setPassword(MD5Util.encode(admin.getPassword()+salt));
        admin.setSalt(salt);
        admin.setIs_enable(true);
//        admin.setLast_login_time(new Date(System.currentTimeMillis()));
        mapper.insertAdmin(admin);

        // 创建 用户角色关联
        Integer adminDBId = admin.getId();
        Integer roleId = admin.getRoleId();
        HashMap<String, Integer> map = new HashMap<>();
        map.put("adminId",adminDBId);
        map.put("roleId",roleId);
        mapper.insertAdminRole(map);
        return true;
    }

    @Transactional
    @Override
    public Boolean updateAdmin(Admin admin) {
        String salt = UUID.randomUUID().toString().replace("-", "");
        admin.setPassword(MD5Util.encode(admin.getPassword()+salt));
        admin.setSalt(salt);
        mapper.update(admin);
        // 修改权限
        mapper.updateRole(admin);
        return true;
    }

    @Override
    public Boolean updateIsEnable(Integer id, Boolean isEnable) {
        mapper.updateIsEnable(isEnable,id);
        return true;
    }

    @Transactional
    @Override
    public Boolean deleteAdminById(Integer id) {
        // 删除管路员用户
        mapper.deleteAdminById(id);
        // 删除用户角色表
        mapper.deleteAdminRoleByAdminId(id);
        return true;
    }
}
